package com.xb.loan.util.binaryConvert.config;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ParseConfig {
	// 字段
	private Field field;
	// 方法名称
	private Method method;
	// set方法名称
	private Method setMethod;
	// 解析的类型
	private ParseType parseType;
	// 注解的顺序
	private int index;
	// list嵌套的层数
	private int listDeep;
	// 值类型，如果是key则为key，map则为map
	private Class<?> valueClass;
	// key类型，如果是key则为key，map则为map
	private Class<?> keyClass;
	
	public ParseConfig(Field field,ParseType parseType,
			int index, int listDeep, Class<?> keyClass, Class<?> valueClass){
		
		super();
		this.field = field;
		this.parseType = parseType;
		this.index = index;
		this.listDeep = listDeep;
		this.valueClass = valueClass;
		this.keyClass = keyClass;
	}
	
	public ParseConfig(Field field, ParseType parseType,int index){
		
		this(field, parseType, index, index, null, null);
	}
	
	public ParseConfig(Method method, Method setMethod, ParseType parseType,
			int index, int listDeep, Class<?> keyClass, Class<?> valueClass) {
		super();
		this.method = method;
		this.setMethod = setMethod;
		this.parseType = parseType;
		this.index = index;
		this.listDeep = listDeep;
		this.valueClass = valueClass;
		this.keyClass = keyClass;
	}

	public ParseConfig(Method method, Method setMethod, ParseType parseType,int index) {
		
		this(method, setMethod, parseType, index, 1, null, null);
	}
	
	public void set(Object obj,Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		
		if(field == null){
			
			this.setMethod.invoke(obj, value);
		} else{
			
			this.field.set(obj, value);
		}
	}
	
	public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		
		if(field == null){
			
			return this.method.invoke(obj);
		} else{
			
			return this.field.get(obj);
		}
	}
	
	public Class<?> getFieldType(){
		
		if(field == null){
			
			return this.method.getReturnType();
		} else{
			
			return this.field.getType();
		}
	}

	public ParseType getParseType() {
		return parseType;
	}
	
	public int getIndex() {
		return index;
	}

	public int getListDeep() {
		return listDeep;
	}

	public Class<?> getValueClass() {
		return valueClass;
	}

	public Class<?> getKeyClass() {
		return keyClass;
	}
}
